package org.elkastud;

import java.awt.Dimension;
import org.elkastud.model.DNASequence;
import org.elkastud.util.PrintComparator;

/**
 * Panel to reprezent compare results
 * @author Mariusz Jasiński
 */
public class ResultPanel extends javax.swing.JPanel {

    /** Creates new form ResultPanel */
    public ResultPanel() {
        initComponents();
        this.seqA.setText("");
        this.seqB.setText("");
        this.seqC.setText("");
        this.compAB.setText("");
        this.compBC.setText("");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        mainLayout = new javax.swing.JPanel();
        seqA = new javax.swing.JLabel();
        seqB = new javax.swing.JLabel();
        seqC = new javax.swing.JLabel();
        compAB = new javax.swing.JLabel();
        compBC = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createEtchedBorder());
        setMaximumSize(new java.awt.Dimension(32767, 134));
        setName("Form"); // NOI18N

        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        jScrollPane1.setMinimumSize(new java.awt.Dimension(6, 136));
        jScrollPane1.setName("jScrollPane1"); // NOI18N

        mainLayout.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        mainLayout.setMaximumSize(new java.awt.Dimension(32767, 134));
        mainLayout.setName("mainLayout"); // NOI18N
        mainLayout.setPreferredSize(new java.awt.Dimension(480, 134));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(org.elkastud.SeqCompApp.class).getContext().getResourceMap(ResultPanel.class);
        seqA.setFont(resourceMap.getFont("seqA.font")); // NOI18N
        seqA.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        seqA.setText(resourceMap.getString("seqA.text")); // NOI18N
        seqA.setAlignmentX(1.0F);
        seqA.setAlignmentY(0.0F);
        seqA.setAutoscrolls(true);
        seqA.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        seqA.setName("seqA"); // NOI18N

        seqB.setFont(resourceMap.getFont("seqA.font")); // NOI18N
        seqB.setText(resourceMap.getString("seqB.text")); // NOI18N
        seqB.setAlignmentX(1.0F);
        seqB.setAlignmentY(0.0F);
        seqB.setAutoscrolls(true);
        seqB.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        seqB.setName("seqB"); // NOI18N

        seqC.setFont(resourceMap.getFont("seqA.font")); // NOI18N
        seqC.setText(resourceMap.getString("seqC.text")); // NOI18N
        seqC.setAlignmentX(1.0F);
        seqC.setAlignmentY(0.0F);
        seqC.setAutoscrolls(true);
        seqC.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        seqC.setName("seqC"); // NOI18N

        compAB.setFont(resourceMap.getFont("seqA.font")); // NOI18N
        compAB.setText(resourceMap.getString("compAB.text")); // NOI18N
        compAB.setAlignmentX(1.0F);
        compAB.setAlignmentY(0.0F);
        compAB.setAutoscrolls(true);
        compAB.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        compAB.setName("compAB"); // NOI18N

        compBC.setFont(resourceMap.getFont("seqA.font")); // NOI18N
        compBC.setText(resourceMap.getString("compBC.text")); // NOI18N
        compBC.setAlignmentX(1.0F);
        compBC.setAlignmentY(0.0F);
        compBC.setAutoscrolls(true);
        compBC.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        compBC.setName("compBC"); // NOI18N

        javax.swing.GroupLayout mainLayoutLayout = new javax.swing.GroupLayout(mainLayout);
        mainLayout.setLayout(mainLayoutLayout);
        mainLayoutLayout.setHorizontalGroup(
            mainLayoutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainLayoutLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainLayoutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(compBC, javax.swing.GroupLayout.DEFAULT_SIZE, 456, Short.MAX_VALUE)
                    .addComponent(seqC, javax.swing.GroupLayout.DEFAULT_SIZE, 456, Short.MAX_VALUE)
                    .addComponent(seqA, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(compAB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(seqB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        mainLayoutLayout.setVerticalGroup(
            mainLayoutLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainLayoutLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(seqA)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(compAB)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(seqB)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(compBC)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(seqC)
                .addContainerGap(17, Short.MAX_VALUE))
        );

        jScrollPane1.setViewportView(mainLayout);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 476, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    /**
     * set sequences to draw
     * @param a sequence
     * @param b sequence
     * @param c sequence
     */
    public void setSequences(DNASequence a,DNASequence b, DNASequence c) {
        this.a = a;
        this.b = b;
        this.c = c;
        drawSequences();
    }

    /**
     * @return remembered sequences
     */
    public DNASequence[] getSequences() {
        return new DNASequence[]{a, b, c};
    }

    /**
     * draw sequences
     */
    public void drawSequences() {
        if (this.a == null || this.b == null || this.c == null) {
            return;
        }
        String text = PrintComparator.getColoredSeq(this.a, true);
        this.seqA.setText(text);
        int width = this.seqA.getGraphics().getFontMetrics().stringWidth(text);
        this.seqA.setPreferredSize(new Dimension(width, this.seqA.getHeight()));

        text = PrintComparator.getColoredSeq(this.b, true);
        this.seqB.setText(text);
        width = this.seqB.getGraphics().getFontMetrics().stringWidth(this.b.toString());
        this.seqB.setPreferredSize(new Dimension(width, this.seqB.getHeight()));

        text = PrintComparator.getColoredSeq(this.c, true);
        this.seqC.setText(text);
        width = this.seqC.getGraphics().getFontMetrics().stringWidth(this.c.toString());
        this.seqC.setSize(width, this.seqC.getHeight());

        text = PrintComparator.compareSequences(this.a, this.b, true);
        this.compAB.setText(text);
        width = Math.max(this.compAB.getGraphics().getFontMetrics().stringWidth(a.toString()), this.compAB.getGraphics().getFontMetrics().stringWidth(b.toString()));
        this.compAB.setSize(width, this.compAB.getHeight());

        text = PrintComparator.compareSequences(this.b, this.c, true);
        this.compBC.setText(text);
        width = Math.max(this.compBC.getGraphics().getFontMetrics().stringWidth(this.b.toString()), this.compBC.getGraphics().getFontMetrics().stringWidth(this.c.toString()));
        this.compBC.setSize(width, this.compBC.getHeight());

        this.mainLayout.setPreferredSize(new Dimension(width * 2, this.getHeight()));

    }

    private DNASequence a;
    private DNASequence b;
    private DNASequence c;

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel compAB;
    private javax.swing.JLabel compBC;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel mainLayout;
    private javax.swing.JLabel seqA;
    private javax.swing.JLabel seqB;
    private javax.swing.JLabel seqC;
    // End of variables declaration//GEN-END:variables

}
